<!-- 查询用户信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="手机号">
                            <a-input v-model="phone" allow-clear placeholder="请输入用户手机号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="姓名">
                            <a-input v-model="userName" allow-clear placeholder="请输入用户姓名" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户ID">
                            <a-input v-model="userId" allow-clear placeholder="请输入用户ID" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户等级">
                            <a-select v-model="grade" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in 10" :key="i" :value="i">{{i === 0 ? '普通会员' : `${i}级`}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="12" :sm="24">
                        <a-form-item label="注册时间">
                            <a-range-picker @change="onChange" allowClear/>
                        </a-form-item>
                    </a-col>
                    <a-col :md="4" :sm="24">
                        <a-form-item >
                            <a-button type="primary" @click="getData()">查询</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small" :scroll="{ x: 1500 }" >
            <div slot="grade" slot-scope="grade">
                <!-- <div v-for="(item) in gradeList" :key="item.id">
                    <span v-if="item.grade == grade">{{item.gradeName}}</span>
                </div> -->
                <div>{{grade === 0 ? '普通会员' : `${grade}级`}}</div>
            </div>
            <template slot="operation" slot-scope="text, record">
                <a-button type="link" @click="relationEvt(record,1)">上级</a-button>
                <a-button type="link" @click="relationEvt(record,2)">下级</a-button>
            </template>
            <template slot="filecoinAddress" slot-scope="text, record">
                <a-button type="link" @click="filecoinAddressEvt(record,1)">查看</a-button>
            </template>
            <template slot="gradeOperation" slot-scope="text, record">
                <a-button type="link" @click="updateGrade(record)">修改</a-button>
            </template>
            <template slot="detail" slot-scope="text, record">
                <a-button type="link" @click="stat(record)">查看</a-button>
            </template>
            <template slot="sonVipOrder" slot-scope="text, record">
                <a-button type="link" @click="sonVipOrder(record)">查看</a-button>
            </template>
            <template slot="userOperation" slot-scope="text, record">
                <a-button type="link" @click="userOperation(record)">代操作</a-button>
            </template>
            <template slot="userNameOperation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="updateUserRealname(record)">修改实名信息</a-button>
                    <a-popconfirm
                        title="是否确认删除当前用户实名信息?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="delUserRealname(record)"
                    >
                        <a-button type="link">删除实名信息</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="查询用户关系"
            :visible="visible"
            width="80%"
            @ok="visible = false"
            @cancel="visible = false"
            >
            <div>
                <relation :parent="parent" :brandId="brandId" />
            </div>
        </a-modal>
        <a-modal
            title="查询用户FIL钱包地址"
            :visible="visibleFilecoinAddress"
            width="80%"
            @ok="visibleFilecoinAddress = false"
            @cancel="visibleFilecoinAddress = false"
            >
            <div>
                <fileCoinAddress :phone="filecoinAddressPhone" :brandId="filecoinAddressBrandId" />
            </div>
        </a-modal>

        <!-- 修改用户等级 -->
        <a-modal
            title="修改用户等级"
            :visible="gradeVisible"
            width="80%"
            @ok="setGrade"
            @cancel="gradeVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户姓名">
                        <a-input v-model="user.userName" disabled placeholder="请输入用户姓名" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户手机号">
                        <a-input v-model="user.phone" disabled placeholder="请输入用户手机号" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户等级">
                        <a-select v-model="user.grade" placeholder="请选择需要修改的等级" allowClear @change="getData">
                            <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 修改用户实名信息 -->
        <a-modal
            title="修改用户实名信息"
            :visible="realnameVisible"
            width="80%"
            @ok="setUserRealname"
            @cancel="realnameVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户姓名">
                        <a-input v-model="user.userName" placeholder="请输入用户姓名" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="用户身份证号">
                        <a-input v-model="user.idNumber" placeholder="请输入用户身份证号" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryMember , updateUserGrade , updateUserRealName } from '@/api/member';
import { queryGradeProduct } from '@/api/product';
import { queryBrand }  from '@/api/brand';
import relation from './components/relation';
import fileCoinAddress from './components/filecoinAddress';
import { setUserIgnorePassword } from '@/api/power';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandList: [],
            isload: false,
            dataList: [],
            columns,
            total: 0,
            page: 1,
            size: 10,
            phone: '',
            userName: '',
            userId: '',
            gtCreateDate: '', // 开始时间
            ltCreateDate: '', // 结束时间
            grade: undefined,
            gradeList: [],
            visible: false,
            parent: {},
            visibleFilecoinAddress: false,
            filecoinAddressPhone: '',
            filecoinAddressBrandId: '',
            gradeVisible: false,
            user: {},
            realnameVisible: false,
        };
    },
    components: {
        relation,
        fileCoinAddress,
    },
    async created(){
        try {
            // await this.getBrand()
            // this.gradeList = await this.getGrade()
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        onChange(date, dateString) {
            console.log(date, dateString);
            this.gtCreateDate = dateString[0]
            this.ltCreateDate = dateString[1]
            this.getData()
        },
        // 查询等级信息
        getGrade(brandId){
            let _this = this
            return new Promise((resolve,reject)=>{
                const params = {
                    'brandId':brandId,
                    'size':99,
                    'page':0,
                }
                queryGradeProduct(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data.content)
                    }else{
                        reject()
                    }
                })
            })
        },
        // 查询会员信息
        getData(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'phone':_this.phone,
                'userName':_this.userName,
                'userId':_this.userId,
                'grade':_this.grade,
                'gtCreateDate':_this.gtCreateDate,
                'ltCreateDate':_this.ltCreateDate,
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryMember(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 查看用户关系
        relationEvt(item,type){
            if(type == 1){
                // 查看上级
                this.parent = new Object({
                    sonUserId: item.userId,
                    parentUserId: '',
                })
                this.visible = true
            }else{
                // 查看下级
                this.parent = new Object({
                    sonUserId: '',
                    parentUserId: item.userId,
                })
                this.visible = true
            }
        },
        // 查看用户FIL钱包地址
        filecoinAddressEvt(item){
            this.filecoinAddressPhone = item.phone
            this.filecoinAddressBrandId = item.brandId
            this.visibleFilecoinAddress = true
        },
        // 修改用户等级
        async updateGrade(user){
            this.user = JSON.parse(JSON.stringify(user))
            // this.gradeVisible = true
            this.gradeList = await this.getGrade(this.user.brandId)
            this.gradeVisible = true
        },
        // 设置用户等级
        setGrade(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'phone':_this.user.phone,
                'grade':_this.user.grade,
                'userId':_this.user.userId,
            }
            updateUserGrade(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.gradeVisible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        stat(item){
            this.$router.push({ path: '/member/stat',query:{userId:item.userId,phone:item.phone,userName:item.userName }})
        },
        sonVipOrder(item){
            this.$router.push({ path: '/member/sonVipOrder',query:{brandId:item.brandId,phone:item.phone,userName:item.userName }})
        },
        // 代操作
        userOperation(user){
            console.log(process.env,'process')
            let url = ''
            const params = {
                account: user.phone,
                brandId: user.brandId
            }
            setUserIgnorePassword(params)
            .then(res=>{
                if(res.success){
                    if(process.env.VUE_APP_API_BASE_URL == '/fileCoinGKApi'){
                        url = 'https://h5.gushouipo.com/xingyaogaoke/xygkNew/#/?brandId=' + user.brandId + '&userId=' + user.userId
                    }else{
                        url = 'https://h5.gushouipo.com/xingyaogaoke/xygkNewtest/#/?brandId=' + user.brandId + '&userId=' + user.userId
                    }
                    window.open(url,'代操作','width=375,height=700,top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
            
        },
        // 修改用户实名信息
        updateUserRealname(item){
            this.user = JSON.parse(JSON.stringify(item));
            this.realnameVisible = true;
        },
        // 确认修改用户实名信息
        setUserRealname(){
            let _this = this;
            if(_this.user.userName == ''){
                _this.$message.warning('请输入用户姓名')
                return;
            }
            if(_this.user.idNumber == ''){
                _this.$message.warning('请输入用户身份证号码')
                return;
            }
            const params = {
                'idNumber': _this.user.idNumber,
                'realName': _this.user.userName,
                'userId': _this.user.userId,
            }
            updateUserRealName(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.realnameVisible = false;
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除用户实名信息
        delUserRealname(item){
            let _this = this;
            const params = {
                'idNumber': item.idNumber,
                'realName': item.userName,
                'userId': item.userId,
                'isDelete': '1'
            }
            updateUserRealName(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>